package com.example.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SettingsDao {
    private mySQLiteOpenHelper helper;
    private SQLiteDatabase db;
    private Context context;

    //构造方法
    public SettingsDao(Context context){
        this.context=context;
    }

    //更新主题
    public void updateTheme(int theme){
        helper=new mySQLiteOpenHelper(context);
        db=helper.getReadableDatabase();

        ContentValues values=new ContentValues();
        values.put("theme",theme);
        db.update("tb_setting",values,"_id=1",null);
        //关闭数据库
        db.close();
        helper.close();
    }

    //更新困难度
    public void updateDifficulty(String difficulty) {
        helper=new mySQLiteOpenHelper(context);
        db=helper.getReadableDatabase();

        ContentValues values=new ContentValues();
        values.put("difficulty",difficulty);
        db.update("tb_setting",values,"_id=1",null);
        //关闭数据库
        db.close();
        helper.close();
    }
    //更新每天需要背的单词数
    public void updateNewNum(int newnum)  {
        helper=new mySQLiteOpenHelper(context);
        db=helper.getReadableDatabase();

        ContentValues values=new ContentValues();
        values.put("newnum",newnum);
        db.update("tb_setting",values,"_id=1",null);
        //关闭数据库
        db.close();
        helper.close();
    }

    //获取目前的主题样式
    public int getTheme(){
        helper=new mySQLiteOpenHelper(context);
        db=helper.getReadableDatabase();
        String sql="select * from tb_setting where _id=1";
        Cursor cursor=db.rawQuery(sql,null);
        if (cursor.moveToNext()){
            int theme=cursor.getInt(cursor.getColumnIndex("theme"));
            //关闭游标和数据库
            cursor.close();
            db.close();
            helper.close();
            return theme;
        }
        //关闭游标和数据库
        cursor.close();
        db.close();
        helper.close();
        return 0;
    }

    //获取难度
    public String getDifficulty() {
        helper=new mySQLiteOpenHelper(context);
        db=helper.getReadableDatabase();
        String sql="select * from tb_setting where _id=1";
        Cursor cursor=db.rawQuery(sql,null);
        if (cursor.moveToNext()){
            String difficulty=cursor.getString(cursor.getColumnIndex("difficulty"));
            //关闭游标和数据库
            cursor.close();
            db.close();
            helper.close();
            return difficulty;
        }
        //关闭游标和数据库
        cursor.close();
        db.close();
        helper.close();
        return null;
    }

    //获取每天需要背的词数量
    public int getNewNum(){
        helper=new mySQLiteOpenHelper(context);
        db=helper.getReadableDatabase();
        String sql="select * from tb_setting where _id=1";
        Cursor cursor=db.rawQuery(sql,null);
        if (cursor.moveToNext()){
            int newnum=cursor.getInt(cursor.getColumnIndex("newnum"));
            //关闭游标和数据库
            cursor.close();
            db.close();
            helper.close();
            return newnum;
        }
        //关闭游标和数据库
        cursor.close();
        db.close();
        helper.close();
        return 0;
    }

}
